package com.example.tellwin.mine.presenter;

import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.bean.ContentBean;
import com.example.tellwin.mine.bean.WeChatPayBean;
import com.example.tellwin.mine.contract.OrderDetailContract;
import com.example.tellwin.utils.HttpUtils;
import com.example.tellwin.utils.ToastUtil;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/12/7
 * @Describe ：
 **/
public class OrderDetailPresenter extends RxPresenter<OrderDetailContract.View> implements OrderDetailContract.Presenter<OrderDetailContract.View> {

    private TwjfApi mApi;

    @Inject
    public OrderDetailPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }

    @Override
    public void getOrderDetail(String orderId) {
        mApi.getOrderDetail(orderId, new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (contentBean.getOrderDetails() == null) {
                    ToastUtil.show(MainApplication.getCurActivity(), R.string.none_data);
                    return;
                }
                mView.orderDetailResult(contentBean.getOrderDetails());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void cancelOrder(String orderId) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId);
        mApi.cancelOrder(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.cancelOrderSuccess();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void meetOrder(String orderId) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId);
        mApi.meetOrder(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.meetOrderResult();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void selectAnswerType(String orderId, int type) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId, "answerType", type + "");
        mApi.selectAnswerType(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.selectAnswerTypeResult(type);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void sendOrder(String orderId) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId);
        mApi.sendOrder(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.sendOrderResult();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void orderPay(String orderId, int type) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId, "payType", type + "");
        mApi.orderPay(requestBody, new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (type == 1) {
                    WeChatPayBean weChatPayBean = contentBean.getWxPayMap();
                    LogUtils.e(weChatPayBean);
                    if (weChatPayBean == null) {
                        ToastUtil.show(MainApplication.getContext(), "获取微信参数失败");
                        return;
                    }
                    mView.onWeChatPay(contentBean.getPayId(), weChatPayBean);
                } else if (type == 2) {
                    if(contentBean.getZfbPayMap()!=null&&!TextUtils.isEmpty(contentBean.getZfbPayMap().data)){
                        mView.onAliPay(contentBean.getZfbPayMap().data);
                    }else {
                        ToastUtil.show(MainApplication.getContext(), "获取支付宝参数失败");
                        return;
                    }
                } else if (type == 3) {
                    mView.onBalancePay();
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void payQuery(String payId, int type) {
        mApi.payQuery(payId, type + "", new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (type == 1) {
                    if (contentBean.getResultMap() != null && contentBean.getResultMap().isSuccess()) {
                        mView.payQuerySuccess();
                    }
                } else if (type == 2) {

                } else if (type == 3) {

                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void orderEvaluate(String orderId, String content, String isSatisfied, String score) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId, "content", content, "isSatisfied", isSatisfied, "score", score);
        mApi.orderEvaluate(requestBody, new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                mView.orderEvaluateSuccess();
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void newOrder(String orderId) {
        RequestBody requestBody = HttpUtils.getRequestBody("orderId", orderId);
        mApi.newOrder(requestBody, new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                mView.newOrderSuccess(contentBean.getOrderId());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
